﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Rhino.Mocks;
using Rhino.Mocks.Interfaces;

using SharedGenomics.Workbench.Core;
using SharedGenomics.Workbench.Services;
using SharedGenomics.Workbench.Views;
using SharedGenomics.Workbench.Presenters;

namespace SharedGenomics.Workbench.Tests.Presenters
{
    [TestClass]
    public class DataSetUploadPresenterTests
    {
        private MockRepository mocks;
        IFileSetUploadView view;
        IOpenGenotypeFilePresenter genotypePresenter;
        IOpenPhenotypeFilePresenter phenotypePresenter;
        IFileSetUploadService fsService;
        IFileUpload fileUpload;

        [TestInitialize]
        public void Setup()
        {
            this.mocks = new MockRepository();

            this.view = this.mocks.DynamicMock<IFileSetUploadView>();
            this.genotypePresenter = this.mocks.StrictMock<IOpenGenotypeFilePresenter>();
            this.phenotypePresenter = this.mocks.StrictMock<IOpenPhenotypeFilePresenter>();
            this.fsService = this.mocks.StrictMock<IFileSetUploadService>();
            this.fileUpload = this.mocks.StrictMock<IFileUpload>();
        }

        [TestMethod]
        public void ConstructorTest()
        {
            IFileSetUploadView view = this.mocks.StrictMock<IFileSetUploadView>();
            IOpenGenotypeFilePresenter genotypePresenter = this.mocks.StrictMock<IOpenGenotypeFilePresenter>();
            IOpenPhenotypeFilePresenter phenotypePresenter = this.mocks.StrictMock<IOpenPhenotypeFilePresenter>();
            ILoginRequiredGuard loginGuard = this.mocks.Stub<ILoginRequiredGuard>();
            IFileSetUploadService fileSetService = this.mocks.Stub<IFileSetUploadService>();
            IFileUpload fileUpload = this.mocks.Stub<IFileUpload>();

            using (this.mocks.Record())
            {
                //presenter should register for the events of the open genotype/phenotype presenters
                genotypePresenter.GenotypeFileLoaded += null;
                LastCall.IgnoreArguments();

                phenotypePresenter.PhenotypeFileLoaded += null;
                LastCall.IgnoreArguments();

                view.UploadButtonClicked += null;
                LastCall.IgnoreArguments();

                //upload button on the view should be disabled initially
                view.IsUploadEnabled = false;
            }

            using (this.mocks.Playback())
            {
                FileSetUploadPresenter p = new FileSetUploadPresenter(view, genotypePresenter, phenotypePresenter, fileSetService, fileUpload);
            }
        }

        [TestMethod]
        [Ignore]
        public void GenotypeFileOnlyLoaded()
        {
        }

        [TestMethod]
        [Ignore]
        public void PhenotypeFileOnlyLoaded()
        {
            
        }

        [TestMethod]
        public void BothFilesLoaded()
        {
            IEventRaiser phenoOpenedEvent, genoOpenedEvent;

            using (this.mocks.Record())
            {
                this.genotypePresenter.GenotypeFileLoaded += null;
                LastCall.IgnoreArguments();
                genoOpenedEvent = LastCall.GetEventRaiser();

                this.phenotypePresenter.PhenotypeFileLoaded += null;
                LastCall.IgnoreArguments();
                phenoOpenedEvent = LastCall.GetEventRaiser();

                this.view.IsUploadEnabled = true;
            }

            using (this.mocks.Playback())
            {
                var presenter = this.CreatePresenter();
                phenoOpenedEvent.Raise(null, null);
                genoOpenedEvent.Raise(null, null);
            }
        }

        [TestMethod]
        [Ignore]
        public void UploadButtonClickedValid()
        {
            //IEventRaiser clickedEvent;
            //string localGenoPath = @"C:\somepath\genotypefile.txt";
            //string remoteGenoPath = "http://someurl.com/genoFilePath.txt";
            //string localPhenoPath = @"C:\somepath\phenotypeFile.csv";
            //string remotePhenoPath = "http://someurl.com/phenoFilePath.csv";
            //string fileSetName = "FSName";

            //using (this.mocks.Record())
            //{
            //    this.view.UploadButtonClicked += null;
            //    LastCall.IgnoreArguments();
            //    clickedEvent = LastCall.GetEventRaiser();

            //    //presenter should check the phenotype data is valid
            //    Expect.Call(this.view.DataSetName).Return(fileSetName);

            //    IErrorNotificationCollection errors;
            //    Expect.Call(this.phenotypePresenter.Validate(out errors)).Return(true).OutRef(null);
            //    Expect.Call(this.phenotypePresenter.PhenotypeFilePath).Return(localPhenoPath);

            //    Expect.Call(this.genotypePresenter.GenotypeFilePath).Return(localGenoPath);
            //    Expect.Call(this.phenotypePresenter.PhenotypeInfo).Return(new PhenotypeFileInfo() { IdColumnName = "Id", SexColumnName = "Sex", MaleSexValue = "M", FemaleSexValue = "F" });

            //    //presenter should create a file set on the server and then upload the genotype and phenotype files
            //    Expect.Call(this.fsService.CreateFileSet(fileSetName)).Return(new ServerAccess.JobService.FileSetUploadInfo_New() { Id = 1, Name = fileSetName, RemoteGenotypeFilePath = remoteGenoPath, RemotePhenotypeFilePath = remotePhenoPath });

            //    this.fileUpload.UploadFile(localGenoPath, remoteGenoPath);
            //    this.fileUpload.UploadFile(localPhenoPath, remotePhenoPath);

            //    //presenter should notify the view the upload has been started
            //    this.view.ServiceMessage = "File upload for file set 'FSName' in progress";
            //}

            //using (this.mocks.Playback())
            //{
            //    var presenter = this.CreatePresenter();

            //    clickedEvent.Raise(null, null);
            //}
        }

        private FileSetUploadPresenter CreatePresenter()
        {
            return new FileSetUploadPresenter(this.view, this.genotypePresenter, this.phenotypePresenter, this.fsService, this.fileUpload);
        }
    }
}
